package com.moss.cloud.plugins.biz.utils;

import com.moss.cloud.plugins.api.model.param.ZipTemplateParam;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class AutoGenerationCode {
    /**
     * 转驼峰
     *
     * @param str
     * @return
     */
    public static String toCamelCase(String str) {
        return Arrays.stream(str.split("_"))
                .map(word -> word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase())
                .collect(Collectors.joining());
    }

    /**
     * 首字母转为小写
     * @param str
     * @return
     */
    public static String toJavaField(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        String newStr = toCamelCase(str);
        return newStr.substring(0, 1).toLowerCase() + newStr.substring(1);
    }

    public static ByteArrayOutputStream createZipFile(List<ZipTemplateParam> params) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (ZipOutputStream zos = new ZipOutputStream(baos)) {
            params.forEach(item -> {
                try {
                    addToZip(item.getTemplateContent(), item.getTemplateName(), zos);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
        return baos;
    }

    private static void addToZip(String content, String fileName, ZipOutputStream zos) throws IOException {
        byte[] bytes = content.getBytes();
        ZipEntry zipEntry = new ZipEntry(fileName);
        zos.putNextEntry(zipEntry);
        zos.write(bytes);
        zos.closeEntry();
    }

    public static void main(String[] args){
        System.out.println(toJavaField("user_name"));
    }
}
